package com.compass.doctor.ui.mine.fragment

import android.os.Bundle
import android.view.View
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.compass.doctor.R
import com.compass.doctor.base.BaseVBFragment
import com.compass.doctor.databinding.FragmentBaseListWhiteBinding
import com.compass.doctor.ui.mine.adapter.UsefulExpressionsAdapter
import com.compass.doctor.ui.mine.viewmodel.UsefulExpressionsViewModel
import com.compass.framework.constant.CHOOSE_USEFUL
import com.compass.framework.constant.KEY_IS_SYSTEM_USEFUL
import com.compass.framework.constant.KEY_USEFUL_CATE
import com.compass.framework.constant.REFRESH_USEFUL
import com.compass.framework.decoration.NormalItemDecoration
import com.compass.framework.ext.divider
import com.compass.framework.ext.gone
import com.compass.framework.ext.load
import com.compass.framework.ext.visible
import com.compass.framework.utils.LiveDataBus
import com.compass.framework.utils.dpToPx
import com.compass.framework.utils.getColorFromResource
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener
import com.scwang.smart.refresh.layout.listener.OnRefreshListener

class UsefulExpressionsFragment : BaseVBFragment<FragmentBaseListWhiteBinding>(), OnLoadMoreListener,
    OnRefreshListener {

    private lateinit var mUsefulAdapter : UsefulExpressionsAdapter
    private val mUsefulExpressionsViewModel:UsefulExpressionsViewModel by viewModels()
    private var mPage = 1

    /**
     * 常用语id
     */
    private var mUsefulCateId: String? = ""
    private var mSystemUseful : Boolean = false
    override fun initView(view: View, savedInstanceState: Bundle?) {
        arguments?.let {
            mUsefulCateId = it.getString(KEY_USEFUL_CATE)
            mSystemUseful = it.getBoolean(KEY_IS_SYSTEM_USEFUL)
        }

        mUsefulExpressionsViewModel.usefulExpressionsLiveData.observe(this){
            dismissLoading()
            if(mPage == 1){
                //第一页数据返回
                mUsefulAdapter.setData(it)
                mBinding?.refreshLayout?.finishRefresh()
                mBinding?.refreshLayout?.setEnableLoadMore(load(it?.size))
                if (it.isNullOrEmpty()) {
                    mBinding?.emptyView?.visible()
                } else {
                    mBinding?.emptyView?.gone()
                }
            }else{
                mBinding?.refreshLayout?.finishRefresh()
                mBinding?.refreshLayout?.finishLoadMore()
                if (!it.isNullOrEmpty()) {
                    mUsefulAdapter.addAll(it)
                } else {
                    mBinding?.refreshLayout?.setEnableLoadMore(false)
                }
            }
        }
        initRecyclerView()

        LiveDataBus.get().with(REFRESH_USEFUL,String::class.java).observe(this){
            if(it == mUsefulCateId){
                mPage = 1
                getUsefulCateList()
            }
        }
    }

     fun initRecyclerView(){
         mBinding?.refreshLayout?.apply {
             setEnableRefresh(true)
             setEnableLoadMore(true)
             setOnRefreshListener(this@UsefulExpressionsFragment)
             setOnLoadMoreListener(this@UsefulExpressionsFragment)
         }
         mUsefulAdapter = UsefulExpressionsAdapter()
         val dp10 = dpToPx(10)
         mBinding?.recyclerView?.apply {

             layoutManager = LinearLayoutManager(context)
             divider(getColorFromResource(R.color.color_d6d6d6), dpToPx(1), false)
             addItemDecoration(NormalItemDecoration().apply {
                 setBounds(left = dp10, top = dpToPx(5), right = dp10, bottom = dpToPx(5))
                 setLastBottom(true)
             })
             adapter = mUsefulAdapter

         }
         mUsefulAdapter.onItemClickListener={ _: View, position: Int ->
            //点击事件以后需要将当前选择的常用语回传至聊天页面输入框
             val content :String? = mUsefulAdapter.getData()[position].content
             LiveDataBus.get().with(CHOOSE_USEFUL).postValue(content)
             activity?.finish()
         }

//         usefulAdapter.setBottomCallBack(object : OnCustomButtonClick {
//             override fun onButtonCallBack(position: Int, msgType: String?) {
//                 val item = prescribedAdapter.getItem(position)
//
//                 when (msgType) {
//                     "cancellation" -> {
//                         item?.let { details ->
//                             cancellationDialog = CancellationDialog.Builder(
//                                 requireActivity(),
//                                 details.id,
//                                 rxCancellationCallBack
//                             )
//                             cancellationDialog?.show()
//
//                         }
//                     }
//
//                     "details" -> {
//                         item?.let { details ->
//
//                             if (details.isTransfer == "1") {
//                                 showToast(getStringFromResource(R.string.prescribed_tips))
//
//                             } else {
//                                 WebDetailActivity.start(
//                                     context = requireActivity(),
//                                     url = RX_DETAILS + details.id,
//                                     title = "处方详情"
//                                 )
//                             }
//                         }
//
//
//                     }
//                 }
//             }
//         })
     }

    override fun initData() {
        super.initData()
        getUsefulCateList()
    }
    private fun getUsefulCateList(){
//        showLoading()
        var typeId = ""
        if(!mUsefulCateId.isNullOrEmpty()){
            typeId = mUsefulCateId as String
        }
        mUsefulExpressionsViewModel.getUsefulExpressionsListByCate(mSystemUseful,typeId,mPage)
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        mPage++
        getUsefulCateList()
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        mPage = 1
        getUsefulCateList()
    }


}